package com.onion.utils.utils;

import org.apache.commons.lang3.StringUtils;
import java.util.regex.Pattern;

public class StringHelper {


    private static final Pattern CHINA_MOBILE_PATTERN = Pattern.compile("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$");

    /**
     * 检查是否为null或空串
     * @param strVal
     * @return
     */
    public static boolean checkBlank(String strVal){
        return strVal==null || strVal.trim().length()==0;
    }

    /**
     * 检查是否为null或空串，或超过最大长度
     * @param strVal
     * @param maxLength
     * @return
     */
    public static boolean checkBlankLen(String strVal, int maxLength){
        if(checkBlank(strVal)){
            return true;
        }
        if(strVal.length() > maxLength){
            return true;
        }
        return false;
    }

    /**
     * 手机号码格式校验
     * @param mobile
     * @return
     */
    public static boolean isMobile(String mobile){
        return StringUtils.isNotBlank(mobile) && CHINA_MOBILE_PATTERN.matcher(mobile).matches();
    }

    /**
     * String转Integer
     * @param value
     * @param defaultValue
     * @return
     */
    public static Integer str2Int(String value, Integer defaultValue){
        if(checkBlank(value)){
            return defaultValue;
        }
        if(isIntValue(value)){
            return Integer.parseInt(value);
        }
        throw new RuntimeException("String转Integer出错！");
    }

    /**
     * 是否整数
     * @return
     */
    public static boolean isInteger(String value){
        if(checkBlank(value)){
            return false;
        }
        return isIntValue(value);
    }

    /**
     * 正则判断整数
     * @param value
     * @return
     */
    private static boolean isIntValue(String value){
        return value.matches("^-?\\d+$");
    }

    /**
     * 校验Email格式校验
     * @param email
     * @return
     */
    public static boolean isEmail(String email){
        return StringUtils.isNotBlank(email) && email.matches("\\w+(\\.\\w)*@\\w+(\\.\\w{2,3}){1,3}");
    }

    /**
     * 定长：不足尾部补充
     * @param value
     * @param fixLength
     * @param appendChar
     * @return
     */
    public static String fixedWithTail(String value, int fixLength, char appendChar){
        String destValue = value==null ? "" : value;
        destValue = destValue.length()>=fixLength ? destValue.substring(0,fixLength) : destValue;
        if(destValue.length() == fixLength){
            return destValue;
        }else{
            StringBuilder strbKey = new StringBuilder(destValue);
            for(int i=0; i<fixLength-destValue.length(); i++){
                strbKey.append(appendChar);
            }
            return strbKey.toString();
        }
    }

    /**
     * 驼峰转下划线
     * @param str
     * @return
     */
    public static String toUnderline(String str) {
        if (str == null || str.trim().length() == 0) {
            return "";
        }
        str = str.trim();
        int len = str.length();
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                if (i > 0) {
                    buffer.append('_');
                }
                buffer.append((char)(ch + 32));
            } else {
                buffer.append(ch);
            }
        }
        return buffer.toString();
    }

    /**
     * 下划线写法转驼峰
     * @param str
     * @return
     */
    public static String toCamel(String str) {
        if (str == null || str.trim().length() == 0) {
            return "";
        }
        str = str.trim();
        int len = str.length();
        StringBuilder buffer = new StringBuilder();
        boolean found = false;
        boolean isFirst = true;
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            if (ch == '_') {
                found = true;
            } else {
                if (!isFirst) {
                    if (found && ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) {
                        found = false;
                        if (ch >= 'a' && ch <= 'z') {
                            buffer.append((char)(ch - 32));
                            continue;
                        }
                    }
                } else {
                    isFirst = false;
                    if (ch >= 'A' && ch <= 'Z') {
                        buffer.append((char)(ch + 32));
                        found = false;
                        continue;
                    }
                }
                found = false;
                buffer.append(ch);
            }
        }
        return buffer.toString();
    }

    public static void main(String[] args){
        System.out.println(fixedWithTail("1111", 16, 'A'));
    }

}
